﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web;
using System.Security.Permissions;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;

namespace Exaction.ListSearch
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    class ListSearchEditorPart : EditorPart
    {
        #region Private Controls
        private DropDownList PageWebPartsDdl;
        #endregion

        ///<summary>
        /// Saves configuration back to web part
        ///</summary>
        ///<returns>true</returns>
        public override bool ApplyChanges()
        {            
           // Saves configuration back to web part
            Exaction.ListSearch.WebParts.ListSearch.ListSearch wp = (Exaction.ListSearch.WebParts.ListSearch.ListSearch)this.WebPartToEdit;
            if (wp != null)
            {
                wp.SelectedWebPartId = PageWebPartsDdl.SelectedValue;
            }
            return true;
        }
 
        ///<summary>
        /// Load configuration, method is unused
        ///</summary>
        public override void SyncChanges()
        {
            Exaction.ListSearch.WebParts.ListSearch.ListSearch webPart = this.WebPartToEdit as Exaction.ListSearch.WebParts.ListSearch.ListSearch; 
            if (webPart != null) 
            {
                PageWebPartsDdl.SelectedValue = webPart.SelectedWebPartId; 
            }
        }
        private void PopulateWebPartsDdl()
        {
            using (SPLimitedWebPartManager wpm = SPContext.Current.Web.GetLimitedWebPartManager(SPUtility.GetPageUrlPath(HttpContext.Current), System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
            {
                 Exaction.ListSearch.WebParts.ListSearch.ListSearch webPart = this.WebPartToEdit as Exaction.ListSearch.WebParts.ListSearch.ListSearch; 
                ListItem item;
                item = new ListItem("--Please select--", "0");
                if (string.IsNullOrEmpty(webPart.SelectedWebPartId) || webPart.SelectedWebPartId.Equals("0"))
                {
                    item.Selected = true;
                }
                PageWebPartsDdl.Items.Add(item);
                
                foreach (System.Web.UI.WebControls.WebParts.WebPart wp in wpm.WebParts)
                {
                    if (wp.GetType().Name.Equals("XsltListViewWebPart"))
                    {
                        item = new ListItem(wp.Title, wp.ID);
                        if (!string.IsNullOrEmpty(webPart.SelectedWebPartId) && webPart.SelectedWebPartId.Equals(wp.ID))
                        {
                            item.Selected = true;
                        }
                        PageWebPartsDdl.Items.Add(item);
                    }
                }
            }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageWebPartsDdl = new DropDownList() { ID = "PageWebPartsDdl" };
        }
        ///<summary>
        /// Create controls for tool part
        ///</summary>
        protected override void CreateChildControls()
        {
            Controls.Clear();
 
            //create controls            
            Controls.Add(PageWebPartsDdl);            
 
            //populate dropdown list
            PopulateWebPartsDdl();
        }
 
        ///<summary>
        /// Create the HTML for the tool part
        ///</summary>
        ///<param name="writer">The HTML writer to output HTML in to</param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.Write("Select the XsltListViewWebPart to filter:");
            writer.WriteBreak();
            writer.WriteBreak();
            PageWebPartsDdl.RenderControl(writer);
            writer.WriteBreak();
            writer.WriteBreak();    
        }
    }
}